home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / utilities / text / less-278.lha / less-278 / src.lha / source / ttyin.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-02-01  |  2.7 KB  |  117 lines

  1. /*
  2.  * Copyright (c) 1984,1985,1989,1994,1995  Mark Nudelman
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright
  11.  *    notice in the documentation and/or other materials provided with 
  12.  *    the distribution.
  13.  *
  14.  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY
  15.  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  16.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
  17.  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE
  18.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
  19.  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 
  20.  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 
  21.  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
  22.  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 
  23.  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN 
  24.  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25.  */
  26.  
  27.  
  28. /*
  29.  * Routines dealing with getting input from the keyboard (i.e. from the user).
  30.  */
  31.  
  32. #include "less.h"
  33.  
  34. static int tty;
  35.  
  36. /*
  37.  * Open keyboard for input.
  38.  */
  39.     public void
  40. open_getchr()
  41. {
  42. #if MSOFTC || OS2
  43.     extern int fd0;
  44.     /*
  45.      * Open a new handle to CON: in binary mode 
  46.      * for unbuffered keyboard read.
  47.      */
  48.      fd0 = dup(0);
  49.      close(0);
  50.      tty = OPEN_TTYIN();
  51. #else
  52.     /*
  53.      * Try /dev/tty.
  54.      * If that doesn't work, use file descriptor 2,
  55.      * which in Unix is usually attached to the screen,
  56.      * but also usually lets you read from the keyboard.
  57.      */
  58.     tty = OPEN_TTYIN();
  59.     if (tty < 0)
  60.         tty = 2;
  61. #endif
  62. }
  63.  
  64. /*
  65.  * Get a character from the keyboard.
  66.  */
  67.     public int
  68. getchr()
  69. {
  70.     char c;
  71.     int result;
  72.  
  73.     do
  74.     {
  75. #if MSOFTC
  76.         /*
  77.          * In raw read, we don't see ^C so look here for it.
  78.          */
  79.         flush();
  80.         c = getch();
  81.         result = 1;
  82.         if (c == '\003')
  83.             return (READ_INTR);
  84. #else
  85. #if OS2
  86.         flush();
  87.         while (_read_kbd(0, 0, 0) != -1)
  88.             continue;
  89.         if ((c = _read_kbd(0, 1, 0)) == -1)
  90.             return (READ_INTR);
  91.         result = 1;
  92. #else
  93.         result = iread(tty, &c, sizeof(char));
  94.         if (result == READ_INTR)
  95.             return (READ_INTR);
  96.         if (result < 0)
  97.         {
  98.             /*
  99.              * Don't call error() here,
  100.              * because error calls getchr!
  101.              */
  102.             quit(QUIT_ERROR);
  103.         }
  104. #endif
  105. #endif
  106.         /*
  107.          * Various parts of the program cannot handle
  108.          * an input character of '\0'.
  109.          * If a '\0' was actually typed, convert it to '\340' here.
  110.          */
  111.         if (c == '\0')
  112.             c = '\340';
  113.     } while (result != 1);
  114.  
  115.     return (c & 0377);
  116. }
  117.